package org.forum.domain;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.struts2.json.annotations.JSON;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


@Entity
@Table(name="user")
public class User implements Serializable{
	
	@Transient
	Logger logger = LoggerFactory.getLogger(User.class);
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(nullable=false,unique=true)
	private Integer userId;
	
	//not null
	@Column(nullable=false,unique=true)
	private String username;
	
	//not null
	@Column(nullable=false)
	private String userPass;
	
	@Column(unique=true)
	private String e_mail;
	
	private Boolean isAdministration;
	
	//the topics of user publish
	
	@OneToMany(targetEntity=MainTopic.class,mappedBy="user")
	private Set<MainTopic> topics = new HashSet<>();

	@ManyToMany(targetEntity=Forum.class)
	@JoinTable(name="user_forum",
	joinColumns=@JoinColumn(name="userId", referencedColumnName="userId"),
	inverseJoinColumns=@JoinColumn(name="forumId", referencedColumnName="forumId"))
	private Set<Forum> forums = new HashSet<>();
	
	@OneToOne(targetEntity=Icon.class)
	@Cascade(CascadeType.ALL)
	//add foreign key
	@JoinColumn(name="iconId", referencedColumnName="iconId", unique=true)
	private Icon icon;

	public Integer getUserId() {
		return userId;
	}

	public void setUserId(Integer userId) {
		this.userId = userId;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getUserPass() {
		return userPass;
	}

	public void setUserPass(String userPass) {
		this.userPass = userPass;
	}

	public String getE_mail() {
		return e_mail;
	}

	public void setE_mail(String e_mail) {
		this.e_mail = e_mail;
	}

	public Boolean getIsAdministration() {
		return isAdministration;
	}

	public void setIsAdministration(Boolean isAdministration) {
		this.isAdministration = isAdministration;
	}
	@JSON(serialize=false)
	public Set<MainTopic> getTopics() {
		return topics;
	}

	public void setTopics(Set<MainTopic> topics) {
		this.topics = topics;
	}
	
	@JSON(serialize=false)
	public Set<Forum> getForums() {
		return forums;
	}

	public void setForums(Set<Forum> forums) {
		this.forums = forums;
	}

	public Icon getIcon() {
		return icon;
	}

	public void setIcon(Icon icon) {
		this.icon = icon;
	}
}
